{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# UNREACHABLE_NODE\n",
    "\n",
    "LangGraph cannot identify an incoming edge to one of your nodes. Check to ensure you have added sufficient edges when constructing your graph.\n",
    "\n",
    "Alternatively, if you are returning [`Command`](/langgraphjs/how-tos/command/) instances from your nodes to make your graphs edgeless, you will need to add an additional `ends` parameter when calling `addNode` to help LangGraph determine the destinations for your node.\n",
    "\n",
    "Here's an example:"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import { Annotation, Command } from \"@langchain/langgraph\";\n",
    "\n",
    "const StateAnnotation = Annotation.Root({\n",
    "  foo: Annotation<string>,\n",
    "});\n",
    "\n",
    "const nodeA = async (_state: typeof StateAnnotation.State) => {\n",
    "  const goto = Math.random() > .5 ? \"nodeB\" : \"nodeC\";\n",
    "  return new Command({\n",
    "    update: { foo: \"a\" },\n",
    "    goto,\n",
    "  });\n",
    "};\n",
    "\n",
    "const nodeB = async (state: typeof StateAnnotation.State) => {\n",
    "  return {\n",
    "    foo: state.foo + \"|b\",\n",
    "  };\n",
    "}\n",
    "\n",
    "const nodeC = async (state: typeof StateAnnotation.State) => {\n",
    "  return {\n",
    "    foo: state.foo + \"|c\",\n",
    "  };\n",
    "}\n",
    "\n",
    "import { StateGraph } from \"@langchain/langgraph\";\n",
    "\n",
    "// NOTE: there are no edges between nodes A, B and C!\n",
    "const graph = new StateGraph(StateAnnotation)\n",
    "  .addNode(\"nodeA\", nodeA, {\n",
    "    // Explicitly specify \"nodeB\" and \"nodeC\" as potential destinations for nodeA\n",
    "    ends: [\"nodeB\", \"nodeC\"],\n",
    "  })\n",
    "  .addNode(\"nodeB\", nodeB)\n",
    "  .addNode(\"nodeC\", nodeC)\n",
    "  .addEdge(\"__start__\", \"nodeA\")\n",
    "  .compile();"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## Troubleshooting\n",
    "\n",
    "The following may help resolve this error:\n",
    "\n",
    "- Make sure that you have not forgotten to add edges between some of your nodes.\n",
    "- If you are returning `Commands` from your nodes, make sure that you're passing an `ends` array with the names of potential destination nodes as shown above."
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "TypeScript",
   "language": "typescript",
   "name": "tslab"
  },
  "language_info": {
   "codemirror_mode": {
    "mode": "typescript",
    "name": "javascript",
    "typescript": true
   },
   "file_extension": ".ts",
   "mimetype": "text/typescript",
   "name": "typescript",
   "version": "3.7.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
